<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="box-card" :body-style="{ 'min-height': 'calc(100vh - 150px)',padding: '20px' }">
          <div slot="header" class="clearfix">
            <span>字典信息</span>
          </div>
          <el-row>
            <el-col :span="24">
              <!-- query -->
              <el-form ref="dictQueryForm" :inline="true" :model="dictListQuery">
                <el-form-item label="字典名称" prop="dictName">
                  <el-input v-model="dictListQuery.dictName" clearable placeholder="请输入数据字典名称" />
                </el-form-item>
                <el-form-item label="字典编码" prop="dictCode">
                  <el-input v-model="dictListQuery.dictCode" clearable placeholder="请输入数据字典编码" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
                  <el-button @click="resetForm('dictQueryForm')">重置</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-button type="primary" icon="el-icon-edit" @click="handleCreate">新增</el-button>
              <!-- table data -->
              <el-table
                v-loading="dictListLoading"
                :data="dictList"
                border
                fit
                highlight-current-row
                header-cell-class-name="table-header-cell"
                style="width: 100%; margin-top:10px;"
                @current-change="handleCurrentChange"
              >
                <el-table-column fixed label="选择" width="50" align="center">
                  <template slot-scope="scope">
                    <el-radio v-model="checkedDict" class="radio" :label="scope.$index">&nbsp;</el-radio>
                  </template>
                </el-table-column>
                <el-table-column
                  fixed
                  prop="sId"
                  label="序号"
                  type="index"
                  width="100px"
                />
                <el-table-column
                  v-if="false"
                  prop="id"
                  label="ID"
                />
                <el-table-column
                  prop="dictName"
                  label="数据字典名称"
                />
                <el-table-column
                  prop="dictCode"
                  label="数据字典编码"
                />
                <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
                  <template slot-scope="{row}">
                    <el-button type="info" size="mini" @click.stop="handleUpdate(row)">修改</el-button>
                    <el-button type="danger" size="mini" @click.stop="handleDelete(row)">删除 </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination v-show="total>0" :total="total" :page.sync="dictListQuery.current" :limit.sync="dictListQuery.size" @pagination="getDictList" />
            </el-col>
          </el-row>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="box-card" :body-style="{ 'min-height': 'calc(100vh - 150px)',padding: '20px' }">
          <div slot="header" class="clearfix">
            <span>字典详情</span>
          </div>
          <div v-if="dictDetailQuery.dictId === ''">
            <div class="my-code">点击字典信息查看详情</div>
          </div>
          <div v-else>
            <el-row>
              <el-col :span="24">
                <el-button type="primary" icon="el-icon-edit" @click="handleDictDetailCreate">添加字典详情</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="handleDictDetailDelete">删除字典详情</el-button>
                <el-table
                  ref="multipleDictDetailTable"
                  v-loading="dictDetailListLoading"
                  :data="dictDetailList"
                  :row-key="getDictDetailRowKeys"
                  border
                  fit
                  highlight-current-row
                  header-cell-class-name="table-header-cell"
                  style="width: 100%; margin-top:10px;"
                  empty-text="请添加字典详情信息"
                  @selection-change="handleDictDetailSelectionChange"
                >
                  <el-table-column
                    type="selection"
                    width="45"
                    reserve-selection
                  />
                  <el-table-column
                    prop="sId"
                    label="序号"
                    type="index"
                    width="100px"
                  />
                  <el-table-column
                    v-if="false"
                    prop="id"
                    label="ID"
                  />
                  <el-table-column
                    prop="dictCode"
                    label="所属字典"
                  />
                  <el-table-column
                    prop="dictDetailName"
                    label="数据名称"
                  />
                  <el-table-column
                    prop="dictDetailValue"
                    label="数据值"
                  />
                  <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
                    <template slot-scope="{row}">
                      <el-button type="info" size="mini" @click.stop="handleDictDetailUpdate(row)">修改</el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <pagination v-show="dictDetailTotal>0" :total="dictDetailTotal" :page.sync="dictDetailQuery.current" :limit.sync="dictDetailQuery.size" @pagination="getDictDetailList" />
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- dialog -->
    <el-dialog :title="dictTextMap[dictDialogStatus]" :visible.sync="dictDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="30%">
      <el-form ref="dictDataForm" :rules="dictAddRules" :model="dictTemp" label-position="left" label-width="90px" style="width: 80%; margin-left:50px;">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="dictTemp.dictName" clearable placeholder="请输入数据字典名称" />
        </el-form-item>
        <el-form-item label="字典编码" prop="dictCode">
          <el-input v-model="dictTemp.dictCode" clearable placeholder="请输入数据字典编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dictDialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dictDialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :title="dictDetailTextMap[dictDetailDialogStatus]" :visible.sync="dictDetailDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="30%">
      <el-form ref="dictDetailDataForm" :rules="dictDetailRules" :model="dictDetailTemp" label-position="left" label-width="90px" style="width: 80%; margin-left:50px;">
        <el-form-item label="数据名称" prop="dictDetailName">
          <el-input v-model="dictDetailTemp.dictDetailName" clearable placeholder="请输入数据名称" />
        </el-form-item>
        <el-form-item label="数据值" prop="dictDetailValue">
          <el-input v-model="dictDetailTemp.dictDetailValue" clearable placeholder="请输入数据值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dictDetailDialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dictDetailDialogStatus==='create'?createDictDeatilData():updateDictDeatilData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { fetchDicts, addDict, deleteDict, updateDict } from '@/api/dict.js'
import { fetchDictDetails, addDictDetail, updateDictDetail, deleteBatchDictDetail } from '@/api/dictDetail.js'
export default {
  name: 'SysDict',
  components: { Pagination },
  data() {
    return {
      dictTextMap: {
        update: '修改',
        create: '新增'
      },
      dictDialogStatus: '',
      dictDialogFormVisible: false,
      dictListLoading: false,
      dictList: [],
      total: 0,
      checkedDict: {},
      dictListQuery: {
        current: 1,
        size: 10,
        dictName: '',
        dictCode: ''
      },
      dictTemp: {
        dictName: '',
        dictCode: ''
      },
      dictAddRules: {
        dictName: [{ required: true, message: '数据字典名称不能为空', trigger: 'change' }],
        dictCode: [{ required: true, message: '数据字典编码不能为空', trigger: 'change' }]
      },
      dictDetailListLoading: false,
      dictDetailList: [],
      dictDetailSelectionList: [],
      dictDetailTotal: 0,
      dictDetailQuery: {
        size: 10,
        current: 1,
        dictId: '',
        dictCode: ''
      },
      dictDetailTextMap: {
        update: '修改',
        create: '新增'
      },
      dictDetailDialogStatus: '',
      dictDetailDialogFormVisible: false,
      dictDetailTemp: {
        dictId: '',
        dictDetailName: '',
        dictDetailValue: ''
      },
      dictDetailRules: {
        dictDetailName: [{ required: true, message: '数据名称不能为空', trigger: 'change' }],
        dictDetailValue: [{ required: true, message: '数据值不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDictList()
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    resetDict() {
      this.dictTemp = {
        dictName: '',
        dictCode: ''
      }
    },
    getDictList() {
      this.dictListLoading = true
      fetchDicts(this.dictListQuery).then(response => {
        const res = response.retBody
        this.dictList = res.records
        this.total = res.total
        this.dictDetailQuery.dictId = ''
        this.dictDetailQuery.dictCode = ''
      }).finally(() => {
        setTimeout(() => {
          this.dictListLoading = false
        }, 1 * 1000)
      })
    },
    handleFilter() {
      this.dictListQuery.current = 1
      this.getDictList()
    },
    handleCurrentChange(val) {
      this.checkedDict = this.dictList.indexOf(val)
      this.dictDetailQuery.dictId = val.id
      this.dictDetailQuery.dictCode = val.dictCode
      this.getDictDetailList()
    },
    handleCreate() {
      this.resetDict()
      this.dictDialogStatus = 'create'
      this.dictDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dictDataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.dictTemp = Object.assign({}, row)
      this.dictDialogStatus = 'update'
      this.dictDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dictDataForm'].clearValidate()
      })
    },
    handleDelete(row) {
      this.$confirm('确定要删除该条字典信息?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDict(row.id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除字典信息成功',
            type: 'success',
            duration: 1000
          })
          this.getDictList()
        })
      }).catch(() => {

      })
    },
    createData() {
      this.$refs['dictDataForm'].validate((valid) => {
        if (valid) {
          addDict(this.dictTemp).then(() => {
            this.dictDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '新增字典信息成功',
              type: 'success',
              duration: 1000
            })
            this.getDictList()
          })
        }
      })
    },
    updateData() {
      this.$refs['dictDataForm'].validate((valid) => {
        if (valid) {
          updateDict(this.dictTemp.id, this.dictTemp).then(() => {
            this.dictDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改字典信息成功',
              type: 'success',
              duration: 1000
            })
            this.getDictList()
          })
        }
      })
    },
    handleDictDetailCreate() {
      this.resetDictDetail()
      this.dictDetailDialogStatus = 'create'
      this.dictDetailDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dictDetailDataForm'].clearValidate()
      })
    },
    handleDictDetailDelete() {
      if (!this.dictDetailSelectionList || this.dictDetailSelectionList.length === 0) {
        this.$notify({
          title: '提示',
          message: '未选择相关字典详情信息',
          type: 'warning',
          duration: 1500
        })
        return
      }

      this.$confirm('确定要删除选中的字典详情信息?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const dictDetailIds = this.dictDetailSelectionList.map(item => {
          return item.id
        })
        const ids = {}
        ids.dictDetailIds = dictDetailIds.join(',')
        deleteBatchDictDetail(ids).then(() => {
          this.$notify({
            title: '成功',
            message: '删除字典详情成功',
            type: 'success',
            duration: 1000
          })
          this.clearMultipleDictDetailTableSelection()
          this.dictDetailQuery.current = 1
          this.getDictDetailList()
        })
      }).catch(() => {

      })
    },
    handleDictDetailUpdate(row) {
      this.dictDetailTemp = Object.assign({}, row)
      this.dictDetailDialogStatus = 'update'
      this.dictDetailDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dictDetailDataForm'].clearValidate()
      })
    },
    getDictDetailRowKeys(row) {
      return row.id
    },
    handleDictDetailSelectionChange(val) {
      this.dictDetailSelectionList = val
    },
    clearMultipleDictDetailTableSelection() {
      this.$refs.multipleDictDetailTable.clearSelection()
      this.dictDetailSelectionList = []
    },
    getDictDetailList() {
      this.dictDetailListLoading = true
      fetchDictDetails(this.dictDetailQuery).then(response => {
        const res = response.retBody
        this.dictDetailList = res.records
        this.dictDetailTotal = res.total
        if (this.dictDetailList && this.dictDetailList.length > 0) {
          this.dictDetailList = this.dictDetailList.map(item => {
            item.dictCode = this.dictDetailQuery.dictCode
            return item
          })
        }
      }).finally(() => {
        setTimeout(() => {
          this.dictDetailListLoading = false
        }, 1 * 1000)
      })
    },
    createDictDeatilData() {
      this.$refs['dictDetailDataForm'].validate((valid) => {
        if (valid) {
          this.dictDetailTemp.dictId = this.dictDetailQuery.dictId
          addDictDetail(this.dictDetailTemp).then(() => {
            this.dictDetailDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '新增字典详情信息成功',
              type: 'success',
              duration: 1000
            })
            this.getDictDetailList()
          })
        }
      })
    },
    updateDictDeatilData() {
      this.$refs['dictDetailDataForm'].validate((valid) => {
        if (valid) {
          updateDictDetail(this.dictDetailTemp.id, this.dictDetailTemp).then(() => {
            this.dictDetailDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改字典详情信息成功',
              type: 'success',
              duration: 1000
            })
            this.getDictDetailList()
          })
        }
      })
    },
    resetDictDetail() {
      this.dictDetailTemp.dictId = ''
      this.dictDetailTemp.dictDetailName = ''
      this.dictDetailTemp.dictDetailValue = ''
    }
  }
}
</script>
<style scoped>
 .my-code{
    padding: 15px;
    line-height: 20px;
    border-left: 3px solid #ddd;
    color: #333;
    font-family: Courier New;
    font-size: 16px
  }
</style>
